package com.dazzle.system.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dazzle.core.util.common.AppUtil;
import com.dazzle.core.util.query.QueryFilter;
import com.dazzle.core.web.filter.security.MySecurityMetadataSource;
import com.dazzle.system.entity.SysResource;
import com.dazzle.system.service.SysResourceService;

import flexjson.JSONSerializer;

@Controller
@RequestMapping("/system")
public class SysResController{
	@Resource
	private SysResourceService sysResService;
	
	@RequestMapping("/reslist")
	public String list(HttpServletRequest request,HttpServletResponse response){
		QueryFilter queryFilter = new QueryFilter(request);
		List<SysResource> list = sysResService.getAll(queryFilter);
		request.setAttribute("resList", list);
		request.setAttribute("pageBean", queryFilter.getPagingBean());
		return "system/sys_res_list";
	}
	@RequestMapping("/add_sys_res")
	public String add(@ModelAttribute("SysResForm") SysResource sysRes) {
		if(sysRes!=null){
			sysResService.save(sysRes);
			
			//修改权限后,要把系统权限列表reload到服务器
			MySecurityMetadataSource dataSource = (MySecurityMetadataSource)AppUtil.getBean("mySecurityMetadataSource");
			dataSource.reloadResourceDefine();
		}
		return "redirect:reslist.do";
	}
	@RequestMapping("/json_get_sys_res")
	public  String get(HttpServletRequest request,HttpServletResponse response) {
		String resId = request.getParameter("editId");
		if(StringUtils.isNotEmpty(resId)){
			SysResource sysRes = sysResService.get(new Long(resId));
			JSONSerializer json = new JSONSerializer();
			request.setAttribute("jsonString", json.serialize(sysRes));
		}
		return "jsonString";
	}
	
	@RequestMapping("/json_del_sys_res")
	public String del(HttpServletRequest request,HttpServletResponse response){
		String ids = request.getParameter("ids");
		StringBuffer msg = new StringBuffer("{'success':'true'");
		if(StringUtils.isNotEmpty(ids)){
			String[] resIds = ids.split(",");
			msg.append(",'msg':'Res ");
			for(String resId : resIds){
				if(StringUtils.isNotEmpty(resId)){
					//SysResource readyToRemove = sysResService.get(new Long(resId));
					//msg.append(readyToRemove.getResName()).append(",");
					//sysResService.flush();
					sysResService.remove(new Long(resId));
				}
			}
			msg.deleteCharAt(msg.length()-1);
			msg.append(" had been remove!'");
		}
		msg.append("}");
		request.setAttribute("jsonString", msg);
		return "jsonString";
	}
	
	@RequestMapping("/json_get_res_tree")
	public String tree(HttpServletRequest request,HttpServletResponse response){
//		 response.setHeader("Cache-Control", "no-cache"); 
//         response.setContentType("text/json;charset=UTF-8"); 
//         try {
//			request.setCharacterEncoding("utf-8");
			String tree = "[{\"text\": \"1.Pre Lunch(120 min)\",\"id\":\"100\",\"hasChildren\":\"false\"}]";//,  'expanded': true,  'classes': 'important',  'children':  [   {    'text': '1.1 The State of the Powerdome (30 min)'},    {    'text': '1.2 The Future of jQuery (30 min)'   },    {    'text': '1.2 jQuery UI - A step to richnessy (60 min)'   }  ] }, {  'text': '2. Lunch  (60 min)' }, {  'text': '3. After Lunch  (120+ min)',  'children':  [   {    'text': '3.1 jQuery Calendar Success Story (20 min)'   },    {    'text': '3.2 jQuery and Ruby Web Frameworks (20 min)'   },    {    'text': '3.3 Hey, I Can Do That! (20 min)'   },    {    'text': '3.4 Taconite and Form (20 min)'   },    {    'text': '3.5 Server-side JavaScript with jQuery and AOLserver (20 min)'   },    {    'text': '3.6 The Onion: How to add features without adding features (20 min)',    'id': '36',    'hasChildren': true   },    {    'text': '3.7 Visualizations with JavaScript and Canvas (20 min)'   },    {    'text': '3.8 ActiveDOM (20 min)'   },    {    'text': '3.8 Growing jQuery (20 min)'   }  ] }]";
//			PrintWriter out = response.getWriter(); 
//			out.write(tree); 
//			out.flush(); 
//			out.close();
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} 
			request.setAttribute("jsonString", tree);
			return "jsonString";
	}
	
}